
var app = getApp()
Page({
  data: {
    goodsList:[],
    payTypePicker:true,
    payTypeChecked:true,
    goodsPriceShow:true,
    goodsInfoShow:true,
    actualPriceShow:true,
    //订单信息
    orderId:'',
    order_no:'',
    order_time:'',
    labor_cost:'',
    product_price:'',
    order_price:'',
    status:'',
    saler:'',
    scanCode:'',
    nickName:'',
    avatarUrl:'',
    isScanCode:false
  },
  onLoad: function (options) {
    app.getMemInfo().then(res=>{
      console.log(res)
      this.setData({
        saler:res.data.name
      })
    })
    let orderId = options.orderId
    // let orderId = 127
    this.setData({
      orderId
    })
    this.getOrderDetail(orderId)
    this.getScanCode(orderId)    
  },
  bindOpenPicker(){
    this.setData({
      payTypePicker:false
    })
  },
  cancel(){
    this.setData({
      payTypePicker:true
    })
  },
  confirm(){
    this.setData({
      payTypePicker:true
    })
  },
  bindChoseWeiPay(){
    this.setData({
      payTypeChecked:true
    })
  },
  bindChoseCashPay(){
    this.setData({
      payTypeChecked:false
    })
  },
  getScanCode(orderId){
    let data = {
      orderId
    }
    app.request('wx_app/pay/qr_url',data,'GET').then(res=>{
      console.log(res)
      this.setData({
        scanCode:res.data
      })
    })
  },
  bindToPayMoney(){
    var that = this
    if (this.data.status=='提交') {
      // if (this.data.payTypeChecked) {
        
      //   let data = {
      //     orderId:this.data.orderId
      //   }
      //   app.request('wx_app/pay/qr_url',data,'GET').then(res=>{
      //     console.log(res)
      //     if (res.message=='成功') {
      //       wx.navigateTo({
      //         url:'scanCode?url='+res.data+'&price='+this.data.order_price
      //       })
      //     }else{
      //       app.errorMsg(res.message)
      //     }
      //   })
      // }else{
        let data = {
                orderId:that.data.orderId
              }
        wx.showModal({
          title: '确认线下结算',
          content: '确定客户已经结算了吗?',
          success: function(res) {
            if (res.confirm) {
              app.request('wx_app/pay/underLine/finish',data,'GET').then(res=>{
                console.log(res)
                if (res.code==1) {
                  wx.switchTab({
                    url:'stock'
                  })
                }else{
                  app.errorMsg(res.message)
                }
              })
            } else if (res.cancel) {
              console.log('用户点击取消')
              app.request('wx_app/pay/underLine/cancel',data,'GET').then(res=>{
                console.log(res)
                if (res.code==0) {
                  
                }
              })
            }
          }
        })
      // }
    }else{
      app.errorMsg('订单已支付')
    }
  },
  getOrderDetail(orderId){
    var that = this
    var data = {
      orderId
    }
    app.request('wx_app/user_order/detail',data,'GET').then(res=>{
      console.log(res)
      let goodsList = []
      res.data.orderDetails.forEach(item=>{
        goodsList.push({
          name:item.name,
          url:item.pic,
          price:item.price,
          actualPrice:item.actualPrice,
          type:item.des,
          num:item.amount,
          companyName:item.productTag,
          color:item.tagColor
        })
      })
      that.setData({
        goodsList,
        labor_cost:res.data.labor_cost?res.data.labor_cost:0,
        order_no:res.data.orderNo,
        order_time:res.data.createTime,
        product_price:res.data.productPrice,
        order_price:res.data.orderPrice,
        status:res.data.orderState
      })
    })
  },
  lookCode(){
    let that = this
    this.setData({
      isScanCode:true
    })
    wx.getUserInfo({
      success(res){
        console.log(res.userInfo)
        that.setData({
          nickName:res.userInfo.nickName,
          avatarUrl:res.userInfo.avatarUrl
        })
      }
    })
  },
  quit(){
    this.setData({
      isScanCode:false
    })
  }
})